<?php
// application/modules/default/controllers/IndexController.php
/**
 * demo-zf application
 *
 * default\IndexController
 *
 * @uses Lib_Default_ActionAbstract
 * @author Adam Pilacki <adam.pilacki@gmail.com>
 */
class IndexController
    extends Lib_Default_ActionAbstract
{
    public function init()
    {
        parent::init();

//        $this->_helper->getHelper('AjaxContext')->addActionContext('contactme', 'html')->initContext();
//        $this->_helper->getHelper('AjaxContext')->addActionContext($actionName, 'json')->initContext();
    }

    /**
     * Action - displays home page
     */
    public function indexAction()
    {
        /**
         * Try debug package
         * Logging to firebug console
         * Logging to file (logs/)
         * BEGIN
         */
        $conn = AdamP_Model_Connection::getConnection('default');
        AdamP_Debug_Firebug::addProfiler($conn);
        AdamP_Debug_File::addProfiler($conn);
        /* sample two queries */
        $conn->query('select * from users where login = ?', array('adam'));
        $conn->query('select * from users where login = ? and id = ?', array('adam', 1));

        AdamP_Debug_Firebug::log('some info message', Zend_Log::INFO);
        AdamP_Debug_Firebug::log('some warn message', Zend_Log::WARN);
//        AdamP_Debug_Firebug::log('some error message', Zend_Log::ERR); // causes firebbug to raise an error - sometimes very usefull
        AdamP_Debug_File::log('some info message', Zend_Log::INFO);
        AdamP_Debug_File::log('some warn message', Zend_Log::WARN);
        /**
         * END
         */

        $this->_sessionNamespace->lastPage = '/';
        $this->view->users = AdamP_Model::getTable('Users')->fetchAll();
    }

    /**
     * Action - displays page with files to download
     */
    public final function downloadAction()
    {
        $this->_sessionNamespace->lastPage = '/index/download';
    }

    /**
     * Action - Ajax - used in accordion on homepage
     */
    public final function contactmeAction()
    {
        Zend_Layout::getMvcInstance()->disableLayout();
//        Zend_Controller_Action_HelperBroker::removeHelper('viewRenderer');
//        $this->_helper->viewRenderer->setNoRender();

        if (!$this->getRequest()->isXmlHttpRequest()) {
            throw new Zend_Controller_Action_Exception('This action must be called by ajax');
        }
    }

//    public final function httpAction()
//    {
//        /* Prepare client and send request */
//        $client = new Zend_Http_Client('http://localhost');
//        $postParams = array();
//        $client->setParameterPost($postParams);
//        $response = $client->request('POST');
//        echo $response->getBody();
//    }

    /**
     * Action - switches app language
     */
    public final function switchLanguageAction()
    {
        $lang = $this->_getParam('lang', Zend_Registry::get('config')->resources->translate->defaultLang);
        if (Resource_Translate::isLanguageValid($lang)) {
            $this->_sessionNamespace->lang = $lang;
        }
        $lastPage = $this->_sessionNamespace->lastPage;
        $this->_sessionNamespace->lastPage = '/';
        $this->_redirect($lastPage);
    }
}